package com.weetop.cfw.mine.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.entity.LocalMedia
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.FeedBackPresenterImp
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.view.FeedBackView
import com.weetop.cfw.bean.UploadImageBean
import com.weetop.cfw.constants.UrlConstants
import com.weetop.cfw.utils.GlideUtils
import com.weetop.cfw.utils.PictureSelectorUtils
import kotlinx.android.synthetic.main.activity_feed_back.*


/**
 * 意见反馈界面
 */
class FeedBackActivity : CommonBaseActivity(),View.OnClickListener,FeedBackView {

    private lateinit var feedBackPresenterImp: FeedBackPresenterImp

    private val imageUrlList = ArrayList<String>()
    private val localMediaSelectList = ArrayList<LocalMedia>()

    private var maxSelectedNumber = 5//图片选择的最大数量

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.imageFeedBackBack -> ActivityUtils.finishActivity(this)
            R.id.btnSubmitFeedbackContent -> {
                val feedbackImageStringBuilder = StringBuilder()
                for(imageUrl in imageUrlList){
                    when {
                        imageUrlList.indexOf(imageUrl) == 0 -> {
                            feedbackImageStringBuilder.append("[${imageUrl},")
                        }
                        imageUrlList.indexOf(imageUrl) == (imageUrlList.size - 1) -> {
                            feedbackImageStringBuilder.append("$imageUrl]")
                        }
                        else -> {
                            feedbackImageStringBuilder.append("$imageUrl,")
                        }
                    }
                }
                feedBackPresenterImp.feedback(this@FeedBackActivity,editFeedBackContent.text.toString().trim(),feedbackImageStringBuilder.toString())
            }
            R.id.imageUploadItem1,R.id.imageUploadItem2,R.id.imageUploadItem3,R.id.imageUploadItem4,R.id.imageUploadItem5 -> PictureSelectorUtils.openAlbumAndCamera(this,maxSelectedNumber)
            R.id.imageClearImage1 -> {
                if(imageUrlList.size >= 1){
                    clearFeedbackImage(0,imageUploadItem1,imageClearImage1)
                }else{
                    clearFeedbackImage(imageUrlList.size - 1,imageUploadItem1,imageClearImage1)
                }
            }
            R.id.imageClearImage2 -> {
                if(imageUrlList.size >= 2){
                    clearFeedbackImage(1,imageUploadItem2,imageClearImage2)
                }else{
                    clearFeedbackImage(imageUrlList.size - 1,imageUploadItem2,imageClearImage2)
                }
            }
            R.id.imageClearImage3 -> {
                if(imageUrlList.size >= 3){
                    clearFeedbackImage(2,imageUploadItem3,imageClearImage3)
                }else{
                    clearFeedbackImage(imageUrlList.size - 1,imageUploadItem3,imageClearImage3)
                }
            }
            R.id.imageClearImage4 -> {
                if(imageUrlList.size >= 4){
                    clearFeedbackImage(3,imageUploadItem4,imageClearImage4)
                }else{
                    clearFeedbackImage(imageUrlList.size - 1,imageUploadItem4,imageClearImage4)
                }
            }
            R.id.imageClearImage5 -> {
                if(imageUrlList.size >= 5){
                    clearFeedbackImage(4,imageUploadItem5,imageClearImage5)
                }else{
                    clearFeedbackImage(imageUrlList.size - 1,imageUploadItem5,imageClearImage5)
                }
            }
        }
    }

    /**
     * 移除图片
     */
    private fun clearFeedbackImage(position:Int,feedbackImage:ImageView,imageClearImage:ImageView){
        imageUrlList.remove(imageUrlList[position])
        GlideUtils.loadImage(feedbackImage,R.mipmap.upload_img_place_holder,feedbackImage)
        feedbackImage.isEnabled = true
        imageClearImage.visibility = View.GONE

        maxSelectedNumber += 1
    }


    override fun getLayoutId() = R.layout.activity_feed_back

    override fun initView(savedInstanceState: Bundle?) {
         setViewsOnClickListener(this,imageFeedBackBack,btnSubmitFeedbackContent,imageUploadItem1,imageUploadItem2,imageUploadItem3,imageUploadItem4,imageUploadItem5,imageClearImage1,
             imageClearImage2,imageClearImage3,imageClearImage4,imageClearImage5)
    }

    override fun initData(savedInstanceState: Bundle?) {
       feedBackPresenterImp = FeedBackPresenterImp()
       feedBackPresenterImp.attachView(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        feedBackPresenterImp.detachView()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                PictureConfig.CHOOSE_REQUEST -> {
                    // 图片选择结果回调
                    val selectList = PictureSelector.obtainMultipleResult(data)
                    // 例如 LocalMedia 里面返回五种path
                    // 1.media.getPath(); 原图path，但在Android Q版本上返回的是content:// Uri类型
                    // 2.media.getCutPath();裁剪后path，需判断media.isCut();切勿直接使用
                    // 3.media.getCompressPath();压缩后path，需判断media.isCompressed();切勿直接使用
                    // 4.media.getOriginalPath()); media.isOriginal());为true时此字段才有值
                    // 5.media.getAndroidQToPath();Android Q版本特有返回的字段，但如果开启了压缩或裁剪还是取裁剪或压缩路径；注意：.isAndroidQTransform(false);此字段将返回空
                    // 如果同时开启裁剪和压缩，则取压缩路径为准因为是先裁剪后压缩
                    localMediaSelectList.addAll(selectList)
                    for(localMedia in selectList){
                        feedBackPresenterImp.uploadImage(this@FeedBackActivity,localMedia.compressPath)
                    }
                }
              }
            }
    }

    /**
     * 展示意见反馈图片
     */
    private fun showFeedbackImages(){
        textUploadImagesNumber.text = "${imageUrlList.size}/5张"
        when (imageUrlList.size) {
            1 -> {
                relativeUploadImageContainer1.visibility = View.VISIBLE
                relativeUploadImageContainer2.visibility = View.VISIBLE

                imageUploadItem1.visibility = View.VISIBLE
                imageUploadItem2.visibility = View.VISIBLE

                imageUploadItem1.isEnabled = false

                imageClearImage1.visibility = View.VISIBLE

                GlideUtils.loadImage(imageUploadItem1,imageUrlList[0],imageUploadItem1)

                maxSelectedNumber = 4
            }
            2 -> {
                relativeUploadImageContainer1.visibility = View.VISIBLE
                relativeUploadImageContainer2.visibility = View.VISIBLE
                relativeUploadImageContainer3.visibility = View.VISIBLE

                imageUploadItem1.visibility = View.VISIBLE
                imageUploadItem2.visibility = View.VISIBLE
                imageUploadItem3.visibility = View.VISIBLE

                imageUploadItem1.isEnabled = false
                imageUploadItem2.isEnabled = false

                imageClearImage1.visibility = View.VISIBLE
                imageClearImage2.visibility = View.VISIBLE

                GlideUtils.loadImage(imageUploadItem1,imageUrlList[0],imageUploadItem1)
                GlideUtils.loadImage(imageUploadItem2,imageUrlList[1],imageUploadItem2)

                maxSelectedNumber = 3
            }
            3 -> {
                relativeUploadImageContainer1.visibility = View.VISIBLE
                relativeUploadImageContainer2.visibility = View.VISIBLE
                relativeUploadImageContainer3.visibility = View.VISIBLE
                relativeUploadImageContainer4.visibility = View.VISIBLE

                imageUploadItem1.visibility = View.VISIBLE
                imageUploadItem2.visibility = View.VISIBLE
                imageUploadItem3.visibility = View.VISIBLE
                imageUploadItem4.visibility = View.VISIBLE

                imageUploadItem1.isEnabled = false
                imageUploadItem2.isEnabled = false
                imageUploadItem3.isEnabled = false

                imageClearImage1.visibility = View.VISIBLE
                imageClearImage2.visibility = View.VISIBLE
                imageClearImage3.visibility = View.VISIBLE

                GlideUtils.loadImage(imageUploadItem1,imageUrlList[0],imageUploadItem1)
                GlideUtils.loadImage(imageUploadItem2,imageUrlList[1],imageUploadItem2)
                GlideUtils.loadImage(imageUploadItem3,imageUrlList[2],imageUploadItem3)

                maxSelectedNumber = 2
            }
            4 -> {
                relativeUploadImageContainer1.visibility = View.VISIBLE
                relativeUploadImageContainer2.visibility = View.VISIBLE
                relativeUploadImageContainer3.visibility = View.VISIBLE
                relativeUploadImageContainer4.visibility = View.VISIBLE
                relativeUploadImageContainer5.visibility = View.VISIBLE

                imageUploadItem1.visibility = View.VISIBLE
                imageUploadItem2.visibility = View.VISIBLE
                imageUploadItem3.visibility = View.VISIBLE
                imageUploadItem4.visibility = View.VISIBLE
                imageUploadItem5.visibility = View.VISIBLE

                imageUploadItem1.isEnabled = false
                imageUploadItem2.isEnabled = false
                imageUploadItem3.isEnabled = false
                imageUploadItem4.isEnabled = false

                imageClearImage1.visibility = View.VISIBLE
                imageClearImage2.visibility = View.VISIBLE
                imageClearImage3.visibility = View.VISIBLE
                imageClearImage4.visibility = View.VISIBLE

                GlideUtils.loadImage(imageUploadItem1,imageUrlList[0],imageUploadItem1)
                GlideUtils.loadImage(imageUploadItem2,imageUrlList[1],imageUploadItem2)
                GlideUtils.loadImage(imageUploadItem3,imageUrlList[2],imageUploadItem3)
                GlideUtils.loadImage(imageUploadItem4,imageUrlList[3],imageUploadItem4)

                maxSelectedNumber = 1
            }
            5 -> {
                relativeUploadImageContainer1.visibility = View.VISIBLE
                relativeUploadImageContainer2.visibility = View.VISIBLE
                relativeUploadImageContainer3.visibility = View.VISIBLE
                relativeUploadImageContainer4.visibility = View.VISIBLE
                relativeUploadImageContainer5.visibility = View.VISIBLE

                imageUploadItem1.visibility = View.VISIBLE
                imageUploadItem2.visibility = View.VISIBLE
                imageUploadItem3.visibility = View.VISIBLE
                imageUploadItem4.visibility = View.VISIBLE
                imageUploadItem5.visibility = View.VISIBLE

                imageUploadItem1.isEnabled = false
                imageUploadItem2.isEnabled = false
                imageUploadItem3.isEnabled = false
                imageUploadItem4.isEnabled = false
                imageUploadItem5.isEnabled = false

                imageClearImage1.visibility = View.VISIBLE
                imageClearImage2.visibility = View.VISIBLE
                imageClearImage3.visibility = View.VISIBLE
                imageClearImage4.visibility = View.VISIBLE
                imageClearImage5.visibility = View.VISIBLE

                GlideUtils.loadImage(imageUploadItem1,imageUrlList[0],imageUploadItem1)
                GlideUtils.loadImage(imageUploadItem2,imageUrlList[1],imageUploadItem2)
                GlideUtils.loadImage(imageUploadItem3,imageUrlList[2],imageUploadItem3)
                GlideUtils.loadImage(imageUploadItem4,imageUrlList[3],imageUploadItem4)
                GlideUtils.loadImage(imageUploadItem4,imageUrlList[4],imageUploadItem5)

                maxSelectedNumber = 0

            }
        }
    }

    /**
     * 意见反馈成功
     */
    override fun feedbackSuccess() {
        showNativeShortToast("意见反馈提交成功")
        ActivityUtils.finishActivity(this)
    }

    /**
     * 图片上传成功
     */
    override fun uploadImageSeccess(uploadImageBean: UploadImageBean?) {
        if(uploadImageBean != null){
            imageUrlList.add("${UrlConstants.baseUrl}${uploadImageBean.imgurl}")
            LogUtils.eTag("imageUrlList",imageUrlList)
            showFeedbackImages()
        }
    }

}
